package com.qdlc.p2p.biz.executer.impl;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.executer.BaseExecuter;
import com.qdlc.p2p.biz.service.UserRedEnvelopeService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.dal.dto.User;

/**
 * 发放用户红包
 * 
 * @author ylx
 * @version 2.0
 * @date 2015年5月28日 下午9:46:13
 */
@Service
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserRedEnvelopeExecuter extends BaseExecuter {

	@Override
	public String getAccountLogType() {
		return null;
	}

	@Override
	public void extend() {
		// 红包ID
		Object redEnvelopeIdObj = Global.getTransfer().get("redEnvelopeId");
		// 发放对象
		Object toUserIdObj = Global.getTransfer().get("toUserId");
		// 红包来源
		Object sourceTypeObj = Global.getTransfer().get("sourceType");
		
		if (redEnvelopeIdObj != null && toUserIdObj != null && sourceTypeObj != null) {
			long redEnvelopeId = NumberUtil.getLong(redEnvelopeIdObj.toString());
			long toUserId = NumberUtil.getLong(toUserIdObj.toString());
			String sourceType = sourceTypeObj.toString();
			
			// 发放红包
			UserRedEnvelopeService userRedEnvelopeService = (UserRedEnvelopeService) BeanUtil.getBean("userRedEnvelopeService");
			userRedEnvelopeService.giveRedEnvelope(toUserId, redEnvelopeId, sourceType);
		}
	}
	
	/**
	 * 根据用户id，投标id获取投标使用红包金额
	 * 
	 * @param tenderId
	 * @return
	 */
	public double getTenderRedEnvelopeAmount(long tenderId) {
		UserRedEnvelopeService userRedEnvelopeService = (UserRedEnvelopeService) BeanUtil.getBean("userRedEnvelopeService");
		return userRedEnvelopeService.getTenderUseAmount(tenderId);
	}
	
	/**
	 * 检测用户红包状态
	 * 
	 * @param redEnvelopeIds
	 * @param user
	 * @return
	 */
	public boolean checkRedEnvelope(long[] redEnvelopeIds, User user) {
		UserRedEnvelopeService userRedEnvelopeService = (UserRedEnvelopeService) BeanUtil.getBean("userRedEnvelopeService");
		return userRedEnvelopeService.checkRedEnvelope(redEnvelopeIds, user);
	}
}
